//
//  LTDatePickerView.m
//  LearnTeacher
//
//  Created by zyl on 2024/4/19.
//

#import "LTDatePickerView.h"

@implementation LTDatePickerView
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = touches.anyObject;
    CGPoint p = [touch locationInView:self];
    if (p.y > self.showTimeV.height) {
        if (!self.showTimeV.hidden) {
            [self dismissView];
        }
    
    }
    
}
- (void)setUI
{
    [self.showTimeV setCorners:UIRectCornerBottomLeft|UIRectCornerBottomRight cornerRadius:20];
    if (!self.datePicker.superview) {
        [self.datePickerV addSubview:self.datePicker];
    }
    self.backgroundColor = rgba(0, 0, 0, 0.60);
    self.showTimeTop.constant = -159;
    
}
- (void)showView
{
    self.hidden = NO;
    self.showTimeV.hidden = NO;
   
    self.showTimeTop.constant = 0;
    [UIView animateWithDuration:0.3 animations:^{
        [self layoutIfNeeded];
    } ];
   
}
- (void)dismissView
{
    self.showTimeTop.constant = - 159;
    self.datePickerBottom.constant = - 240;
    [UIView animateWithDuration:0.2 animations:^{
        [self layoutIfNeeded];
    } completion:^(BOOL finished) {
        self.hidden = YES;
        self.showTimeV.hidden = YES;
        self.datePickerV.hidden = YES;
        if (self.block) {
            self.block(@"");
        }
    }];
    
}
- (void)showDatePickerView
{
    self.datePickerV.hidden = NO;
    self.datePickerBottom.constant = 0;
    [UIView animateWithDuration:0.3 animations:^{
        [self layoutIfNeeded];
    }];
}
- (void)dismissDatePickerView
{
    self.datePickerBottom.constant = - 240;
    [UIView animateWithDuration:0.15 animations:^{
        [self layoutIfNeeded];
    } completion:^(BOOL finished) {
    }];
}
- (DatePickerView *)datePicker
{
    if (!_datePicker) {
        _datePicker = [[DatePickerView alloc] init];
        _datePicker.backGroundColor = LTWhiteColor;
        [_datePicker configPickView];
        
    }
    return _datePicker;
  
}
- (IBAction)clearAction:(UIButton *)sender {
    self.startDate.text = @"";
    self.endDate.text = @"";
}

- (IBAction)confirmAction:(UIButton *)sender {
    if (self.block) {
        self.block([NSString stringWithFormat:@"%@+%@", self.startDate.text, self.endDate.text]);
    }
    [self dismissView];
}
- (IBAction)cancelAction:(UIButton *)sender {
    [self dismissDatePickerView];
}
- (IBAction)confirmDateAction:(UIButton *)sender {
    
    NSDate *date = [self.datePicker getDateByPickerview];
    if (self.type == 1) {
        self.startDate.text = [MethodTool dateToStyle1WithDate:[date timeIntervalSince1970]];
    }
    if (self.type == 2) {
        self.endDate.text = [MethodTool dateToStyle1WithDate:[date timeIntervalSince1970]];
    }
    [self dismissDatePickerView];
}

- (IBAction)goSelectStartDateAction:(UIButton *)sender {
    self.type = 1;
    [self showDatePickerView];
}

- (IBAction)goSelectEndDateAction:(UIButton *)sender {
    self.type = 2;
    [self showDatePickerView];
}
@end
